//
//  MyCommissionVC.m
//  WanCaiC8
//
//  Created by tangang on 2018/1/19.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "MyCommissionVC.h"
#import "TransactionRecordsCell.h"
#import "BindBankCardVC.h"
#import "WithdrawalVC.h"
#import "MyVM.h"
#import "MJRefresh.h"
@interface MyCommissionVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) MyCommissionModel *commissionModel;
@property (nonatomic ,strong) NSMutableArray *dataArr;

@property (nonatomic ,assign)NSInteger   page;
@end

@implementation MyCommissionVC{
    
    ///1=收入明细 2=提现明细
    NSInteger   type;
    UIButton *btnIncome;
    UIView   *redLine;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"我的佣金";
    UIBarButtonItem *rightItem=[[UIBarButtonItem alloc]initWithTitle:@"绑定账户" style:UIBarButtonItemStylePlain target:self action:@selector(onclickBand)];
    self.navigationItem.rightBarButtonItem=rightItem;
    [self getData];
}

- (void)getData{
    WS(weakSelf);
    type=1;
    [[MyVM manage] getMyCommissionSuccess:^(id returnValue) {
        weakSelf.commissionModel=returnValue;
        [weakSelf headView];
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.header beginRefreshing];
    } andFail:^(NSString *errorMessage, NSError *error) {
        
    }];
}

- (void)headView{
    WS(weakSelf);
    UIView *topView=[[UIView alloc]init];
    topView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(weakSelf.view);
        make.height.mas_equalTo(@180);
    }];
    
    QXLable  *labCommission =[[QXLable alloc]initWithFount:18];
    labCommission.textColor=DEFAULTTEXTBLACKCOLOR;
    labCommission.numberOfLines=0;
    NSMutableAttributedString *attStr=[[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@\n我的佣金",self.commissionModel.MyYj]];
    [attStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(attStr.length-4, 4)];
    [attStr addAttribute:NSForegroundColorAttributeName value:DEFAULTTEXTGRAYCOLOR range:NSMakeRange(attStr.length-4, 4)];
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paraStyle.lineSpacing = 5; //设置行间距
    [attStr addAttribute:NSParagraphStyleAttributeName value:paraStyle range:NSMakeRange(0, attStr.length)];
    labCommission.attributedText=attStr;
    [topView addSubview:labCommission];
    [labCommission mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topView).with.offset(13);
        make.top.equalTo(topView);
        make.size.mas_equalTo(CGSizeMake(120, 66));
    }];
    
    UIImageView *imageV=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"question_mark"]];
    [topView addSubview:imageV];
    [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topView).with.offset(66);
        make.bottom.equalTo(labCommission).with.offset(-14);
        make.size.mas_equalTo(imageV.image.size);
    }];
    
    UIButton *btnWithdrawal =[[UIButton alloc]init];
    [btnWithdrawal setTitle:@"提现" forState:UIControlStateNormal];
    [btnWithdrawal addTarget:self action:@selector(onclockWithdrawal) forControlEvents:UIControlEventTouchUpInside];
    btnWithdrawal.backgroundColor=NAVCOLOR;
    [btnWithdrawal setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btnWithdrawal.layer.masksToBounds=YES;
    btnWithdrawal.layer.cornerRadius=2.0;
    [btnWithdrawal.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [topView addSubview:btnWithdrawal];
    [btnWithdrawal mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(labCommission);
        make.right.equalTo(topView).with.offset(-13);
        make.size.mas_equalTo(CGSizeMake(50, 23));
    }];
    
    UIView *line=[[UIView alloc]init];
    line.backgroundColor=DEFAULTBGCOLOR;
    [topView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(topView);
        make.top.equalTo(topView).with.offset(70);
        make.height.mas_equalTo(@1);
    }];
    
    QXLable *labWithdrawal=[[QXLable alloc]initTitle];
    labWithdrawal.numberOfLines=0;
    labWithdrawal.textAlignment=NSTextAlignmentCenter;
    NSMutableAttributedString *attStr1=[[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@\n可提现",self.commissionModel.KeTx]];
    [attStr1 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(attStr1.length-3, 3)];
    [attStr1 addAttribute:NSForegroundColorAttributeName value:DEFAULTTEXTGRAYCOLOR range:NSMakeRange(attStr1.length-3, 3)];
    NSMutableParagraphStyle *paraStyle1 = [[NSMutableParagraphStyle alloc] init];
    paraStyle1.lineBreakMode = NSLineBreakByCharWrapping;
    paraStyle1.lineSpacing = 8; //设置行间距
    paraStyle1.alignment=NSTextAlignmentCenter;
    [attStr1 addAttribute:NSParagraphStyleAttributeName value:paraStyle1 range:NSMakeRange(0, attStr1.length)];
    labWithdrawal.attributedText=attStr1;
    [topView addSubview:labWithdrawal];
    [labWithdrawal mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topView);
        make.top.equalTo(line.mas_bottom);
        make.width.equalTo(topView).with.multipliedBy(1/3.0);
        make.height.mas_equalTo(@60);
    }];
    
    QXLable *labWithdrawaling=[[QXLable alloc]initTitle];
    labWithdrawaling.numberOfLines=0;
    labWithdrawaling.textAlignment=NSTextAlignmentCenter;
    NSMutableAttributedString *attStr2=[[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@\n提现中",self.commissionModel.Txing]];
    [attStr2 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(attStr2.length-3, 3)];
    [attStr2 addAttribute:NSForegroundColorAttributeName value:DEFAULTTEXTGRAYCOLOR range:NSMakeRange(attStr2.length-3, 3)];
    [attStr2 addAttribute:NSParagraphStyleAttributeName value:paraStyle1 range:NSMakeRange(0, attStr2.length)];
    labWithdrawaling.attributedText=attStr2;
    [topView addSubview:labWithdrawaling];
    [labWithdrawaling mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(topView);
        make.top.equalTo(line.mas_bottom);
        make.width.equalTo(topView).with.multipliedBy(1/3.0);
        make.height.mas_equalTo(@60);
    }];
    
    QXLable *labCount=[[QXLable alloc]initTitle];
    labCount.numberOfLines=0;
    labCount.textAlignment=NSTextAlignmentCenter;
    NSMutableAttributedString *attStr3=[[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@\n累计提现",self.commissionModel.Txleiji]];
    [attStr3 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(attStr3.length-4, 4)];
    [attStr3 addAttribute:NSForegroundColorAttributeName value:DEFAULTTEXTGRAYCOLOR range:NSMakeRange(attStr3.length-4, 4)];
    [attStr3 addAttribute:NSParagraphStyleAttributeName value:paraStyle1 range:NSMakeRange(0, attStr3.length)];
    labCount.attributedText=attStr3;
    [topView addSubview:labCount];
    [labCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(topView);
        make.top.equalTo(line.mas_bottom);
        make.width.equalTo(topView).with.multipliedBy(1/3.0);
        make.height.mas_equalTo(@60);
    }];
    
    UIView *line1=[[UIView alloc]init];
    line1.backgroundColor=DEFAULTBGCOLOR;
    [topView addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(topView);
        make.top.equalTo(labCount.mas_bottom);
        make.height.mas_equalTo(@10);
    }];
    
    btnIncome=[self createBtnWithTitle:@"收入明细"];
    btnIncome.tag=1;
    [topView addSubview:btnIncome];
    [btnIncome mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.equalTo(topView);
        make.top.equalTo(line1.mas_bottom);
        make.width.equalTo(topView).with.multipliedBy(1/2.0);
    }];
    
    redLine=[[UIView alloc]init];
    redLine.backgroundColor=NAVCOLOR;
    [topView addSubview:redLine];
    [redLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(btnIncome);
        make.bottom.equalTo(btnIncome);
        make.size.mas_equalTo(CGSizeMake(44, 2));
    }];
    
    UIButton *btnMakeMoney=[self createBtnWithTitle:@"提现明细"];
    btnMakeMoney.tag=2;
    [topView addSubview:btnMakeMoney];
    [btnMakeMoney mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.equalTo(topView);
        make.top.equalTo(line1.mas_bottom);
        make.width.equalTo(topView).with.multipliedBy(1/2.0);
    }];
}

- (UIButton *)createBtnWithTitle:(NSString *)title{
    UIButton *btn=[[UIButton alloc]init];
    [btn addTarget:self action:@selector(onclickTop:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:DEFAULTTEXTGDARKRAYCOLOR forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:15]];
    return btn;
}

#pragma mark -- 获取收入提现明细
- (void)getMyCommissionList{
    WS(weakSelf);
    [[MyVM manage] getMyCommissionWithType:type andPage:self.page andSuccess:^(id returnValue) {
        NSArray *arr=returnValue;
        if (arr.count<[defaultPageSize integerValue]) {
            [weakSelf.tableView.footer noticeNoMoreData];
        }else{
            [weakSelf.tableView.footer endRefreshing];
        }
        [weakSelf.dataArr addObjectsFromArray:returnValue];
        [weakSelf.tableView.header endRefreshing];
        weakSelf.page+=1;
        [weakSelf.tableView reloadData];
    } andFail:^(NSString *errorMessage, NSError *error) {
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
    }];
}


- (void)onclickTop:(UIButton *)sender{
    type=sender.tag;
    if (type==1) {
        [redLine mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(btnIncome);
        }];
    }else{
        [redLine mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(btnIncome).with.offset(SCREEN_WIDTH/2.0);
        }];
    }
    [self.tableView.header beginRefreshing];
    
}


#pragma mark -- 绑定账户
- (void)onclickBand{
    BindBankCardVC *vc=[[UIStoryboard storyboardWithName:@"My" bundle:nil] instantiateViewControllerWithIdentifier:@"BindBankCardVC"];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -- 提现
- (void)onclockWithdrawal{
    if ([self.commissionModel.KeTx integerValue]<=0) {
        [SVProgressHUD showImage:nil status:@"没有可提现金额"];
        return;
    }
    WithdrawalVC *vc=[[UIStoryboard storyboardWithName:@"My" bundle:nil] instantiateViewControllerWithIdentifier:@"WithdrawalVC"];
    vc.model=self.commissionModel;
    WS(weakSelf);
    vc.bindCard = ^{
        BindBankCardVC *vc=[[UIStoryboard storyboardWithName:@"My" bundle:nil] instantiateViewControllerWithIdentifier:@"BindBankCardVC"];
        [weakSelf.navigationController pushViewController:vc animated:YES];
        [SVProgressHUD showImage:nil status:@"您未绑定银行卡，请先绑定银行卡在进行提现"];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -- UITableViewDelegate ,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TransactionRecordsCell *cell=[tableView dequeueReusableCellWithIdentifier:@"TransactionRecordsCell" forIndexPath:indexPath];
    cell.recordsModel=self.dataArr[indexPath.row];
    return cell;
}

#pragma mark -- 懒加载
- (UITableView *)tableView{
    if (!_tableView) {
        WS(weakSelf);
        _tableView=[[UITableView alloc]init];
        _tableView.dataSource=self;
        _tableView.delegate=self;
        _tableView.backgroundColor=DEFAULTBGCOLOR;
        _tableView.rowHeight=UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight=70;
        [_tableView registerClass:[TransactionRecordsCell class] forCellReuseIdentifier:@"TransactionRecordsCell"];
        _tableView.header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakSelf.dataArr removeAllObjects];
            [weakSelf.tableView reloadData];
            weakSelf.page=1;
            [weakSelf getMyCommissionList];
        }];
        _tableView.footer=[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [weakSelf getMyCommissionList];
        }];
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(weakSelf.view).with.
            insets(UIEdgeInsetsMake(181, 0, 0, 0));
        }];
    }
    return _tableView;
}

- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr=[[NSMutableArray alloc]init];
    }
    return _dataArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
